package com.tourlaid.mamagerment.ao.pc.impl

import com.github.pagehelper.Page
import com.github.pagehelper.PageHelper
import com.tourlaid.mamagerment.ao.pc.StockOutAo
import com.tourlaid.mamagerment.dto.StockOutDto
import com.tourlaid.mamagerment.entity.ResponseData
import com.tourlaid.mamagerment.entity.StockOut
import com.tourlaid.mamagerment.service.*
import com.tourlaid.mamagerment.util.DateUtil
import com.tourlaid.mamagerment.vo.StockOutListVo
import com.tourlaid.mamagerment.vo.StockOutVo
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.util.CollectionUtils
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/23.
 */
@Service("stockOutAo")
class StockOutAoImpl : StockOutAo {

    @Resource
    var stockOutService: StockOutService? = null
    @Resource
    var stockInService: StockInService? = null
    @Resource
    var goodsService: GoodsService? = null
    @Resource
    var supplierService: SupplierService? = null
    @Resource
    var shopService: ShopService? = null
    @Resource
    var shopGoodsService: ShopGoodsService? = null

    override fun list(shopId: Long?, goodsId: Long?,
                      pageNo: Int, pageSize: Int): ResponseData<ArrayList<StockOutListVo>> {
        var voList = ArrayList<StockOutListVo>()
        var responseData = ResponseData(voList)
        PageHelper.startPage<StockOut>(pageNo, pageSize, true)
        var page = stockOutService!!.queryForList(shopId, goodsId)!! as Page<StockOutDto>
        responseData.totalCount = page.getTotal()
        val dtoList = page.getResult()
        if (!CollectionUtils.isEmpty(dtoList)) {
            val shopIdList = dtoList.map { dto -> dto.shopId }
            val goodsIdList = dtoList.map { dto -> dto.goodsId }
            val supplierIdList = dtoList.map { dto -> dto.supplierId }
            val shopList = shopService!!.queryByIdList(shopIdList)
            val goodsList = goodsService!!.queryByIdList(goodsIdList)
            val supplierList = supplierService!!.queryByIdList(supplierIdList)
            val shopMap = shopList.associate { Pair(it.id, it.shopName) }
            val goodsMap = goodsList.associate { Pair(it.id, it.goodsName) }
            val supplierMap = supplierList.associate { Pair(it.id, it.supplierName) }
            for (dto in dtoList) {
                var vo = StockOutListVo()
                BeanUtils.copyProperties(dto, vo)
                vo.shopName = shopMap.get(dto.shopId)
                vo.goodsName = goodsMap.get(dto.goodsId)
                vo.supplierName = supplierMap.get(dto.supplierId)
                vo.createTime = DateUtil.getFormantDate(dto.createTime, DateUtil.DATA_FORMANT)
                voList.add(vo)
            }
        }
        return responseData
    }

    @Transactional
    override fun insert(controlNo: String?, shopId: Long?, stockInId: Long?, stock: Int?) {
        stockOutService!!.insert(controlNo, shopId, stockInId, stock)
        //对应入库单减去出去库存
        stockInService!!.minusStock(stockInId, stock)
        //店铺商品增加库存
        shopGoodsService!!.plusStock(shopId, stockInId!!, stock);
    }

    override fun detail(stockInId: Long): StockOutVo? {
        var vo = StockOutVo()
        var stockOut = stockOutService!!.getById(stockInId)
        stockOut ?: return vo
        BeanUtils.copyProperties(stockOut, vo)
        val stockIn = stockInService!!.getById(stockOut.stockInId!!)
        stockIn ?: return vo
        val goods = goodsService!!.getById(stockIn.goodsId!!)
        vo.goodsName = goods.goodsName
        val supplier = supplierService!!.getById(stockIn.supplierId)
        vo.supplierName = supplier!!.supplierName
        return vo
    }

    override fun update(stockOutId: Long?, shopId: Long?, controlNo: String?) {
        stockOutService!!.update(stockOutId, shopId, controlNo)
    }

    override fun delete(stockOutId: Long?) {
        stockOutId ?: return
        var stockOut = stockOutService!!.getById(stockOutId)
        stockOutService!!.delete(stockOutId)
        stockOut ?: return
        //入库单加库存
        stockInService!!.addStock(stockOut.stockInId, stockOut.stock)
        //门店商品减库存
        shopGoodsService!!.minusStock(stockOut.shopId, stockOut.stockInId!!, stockOut.stock)
    }
}